﻿using System;
using UnityEngine;
using UnityEngine.UIElements;
using winS.UnityEditor.UIElement;
using Button = winS.UnityEditor.UIElement.Button;
using Label = winS.UnityEditor.UIElement.Label;

namespace winS.UnityEditor.ProcessEditor
{
    public class FileChangeDialogueBox : VisualElement
    {
        private Action noCallback;
        private Action yesCallback;

        private readonly Button noButton;
        private readonly Button yesButton;
        private readonly Label label;

        public FileChangeDialogueBox()
        {
            name = "文件变化对话框";
            style.position = Position.Absolute;
            style.justifyContent = Justify.Center;
            style.SetBackgroundColor(0, 0, 0, 128);
            this.StretchToParentSize();

            VisualElement box = EditorElementUtility.elementFactory.CreateEditorElement(new Color32(222, 222, 222, 255));
            box.style.alignSelf = Align.Center;
            box.style.justifyContent = Justify.SpaceAround;
            box.style.SetMinWidth(400f);
            box.style.SetMinHeight(120f);
            box.style.SetHorizontalPadding(8f);
            Add(box);

            box.Add(label = EditorElementUtility.elementFactory.CreateLabel(14f, TextAnchor.UpperLeft));
            label.style.SetColor(22, 22, 22, 255);
            label.style.alignSelf = Align.Auto;

            yesButton = EditorElementUtility.elementFactory.CreateButton("是", () =>
            {
                Hide();
                yesCallback?.Invoke();
            });
            yesButton.style.marginRight = 10f;
            noButton = EditorElementUtility.elementFactory.CreateButton("否", () =>
            {
                Hide();
                noCallback?.Invoke();
            });
            Row row = EditorElementUtility.MergeToRow(noButton, yesButton);
            row.style.flexDirection = FlexDirection.RowReverse;
            box.Add(row);
        }

        public void Show(string content, string yesText, Action yesCallback, string noText, Action noCallback)
        {
            label.text = content;
            noButton.text = noText;
            yesButton.text = yesText;
        
            this.noCallback = noCallback;
            this.yesCallback = yesCallback;
            style.display = DisplayStyle.Flex;
        }
        public void Hide()
        {
            style.display = DisplayStyle.None;
        }
    }
}
